Imports System.IO
Imports System.Globalization
Imports system.Threading

Public Class CryptorMain
  Shared bEncrypt As Boolean = True

  <STAThread()> _
  Shared Sub Main(ByVal args() As String)

    Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture
    Application.EnableVisualStyles()
    'Application.DoEvents()

    ' args: filename, encrypt (1)
    Select Case args.Length
      Case 1
      Case 2
        If Not IsNumeric(args(1)) Then
          MessageBox.Show("Invalid input arguments: " & args(1) & " cannot be converted to an integer" & vbCrLf & vbCrLf & "Required Argument format: Cryptor.exe [<file_to_use> [<1 -> encrypting>]]", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Return
        End If
    End Select
    Dim frm As New frmSimpleFile()
    Dim file As String = GetLongName(args(0))
    If Not IO.File.Exists(file) Then
      MessageBox.Show("The file '" & file & "' does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
      'cant just exit app here, as it wont work.
      'set isOK to false so the the Sub Main that has initialised this form
      'can see that something is wrong, and quit
      Return
    End If
    bEncrypt = String.Compare(Path.GetExtension(file), ".enc", True) <> 0
    If args.Length > 1 Then
      bEncrypt = args(1) = 1
    End If
    'Me.Text = "Encrypt File '" & IO.Path.GetFileName(file) & "'"
    frm.EncryptionWorker.SetData(file, bEncrypt, frm.chkFilenameEnc.Checked)
    frm.EncryptionWorker.Encrypt = bEncrypt
    If Not bEncrypt Then
      frm.SetDecryptUI()
    End If

    ' Try
    Application.Run(frm)
    ' Catch ex As Exception
    'End Try
    'If frm.ShowDialog() = DialogResult.Cancel Then Exit Sub
    ''//Transform the file
    'Dim threadEnc As New Threading.Thread(AddressOf RunTransform)
    'threadEnc.Name = "CryptorBackThread"
    'threadEnc.Start()
    ''routines.CancelTransform()
    'End If
  End Sub

  Shared Function IsRunningXP() As Boolean
    Dim os As OperatingSystem = Environment.OSVersion
    Return os.Version.Major >= 5 AndAlso os.Version.Minor >= 1
  End Function


  Declare Function GetLongPathName Lib "kernel32" _
      Alias "GetLongPathNameA" (ByVal lpszShortPath As String, _
      ByVal lpszLongPath As String, ByVal cchBuffer As Integer) As Integer

  Public Shared Function GetLongName(ByVal sShortFileName As String) As String
    Dim lRetVal As Long, sShortPathName As String, iLen As Integer
    'Set up buffer area for API function call return
    sShortPathName = Space(255)
    iLen = Len(sShortPathName)

    'Call the function
    lRetVal = GetLongPathName(sShortFileName, sShortPathName, iLen)
    'Strip away unwanted characters.
    Return Left(sShortPathName, lRetVal)
  End Function

End Class